package com.sz10.p2p.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.sz10.p2p.Constants;
import com.sz10.p2p.model.LoanInfo;
import com.sz10.p2p.service.LoanInfoService;
import com.sz10.p2p.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Reference(interfaceClass = LoanInfoService.class, version = "1.0.0", check = false)
    private LoanInfoService loanInfoService;

    @Reference(interfaceClass = UserService.class, version = "1.0.0", check = false, timeout = 15000)
    private UserService userService;

    @RequestMapping("/index")
    public String toIndex(Model model) {

        //平台历史年化收益率
        Double historyAvgRate = loanInfoService.queryHistoryAvgRate();
        model.addAttribute(Constants.HISTORY_AVG_RATE, historyAvgRate);

        //平台用户数
        Integer allUserCount = userService.queryAllUserCount();
        model.addAttribute(Constants.ALL_USER_COUNT, allUserCount);

        //平台累计提交金额
        Double allBidMoney = loanInfoService.queryAllBidMoney();
        model.addAttribute(Constants.ALL_BID_MONEY, allBidMoney);

        //获取新手宝产品信息
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put(Constants.CURRENT_PAGE, 0);
        paramMap.put(Constants.PRODUCT_TYPE, Constants.X);
        paramMap.put(Constants.PAGE_SIZE, 1);

        List<LoanInfo> loanInfoX = loanInfoService.queryLoanInfoListByProductType(paramMap);
        model.addAttribute(Constants.LOAN_INFO_X, loanInfoX);

        //获取优选产品信息
        paramMap.put(Constants.PRODUCT_TYPE, Constants.Y);
        paramMap.put(Constants.PAGE_SIZE, 4);
        List<LoanInfo> loanInfoY = loanInfoService.queryLoanInfoListByProductType(paramMap);
        model.addAttribute(Constants.LOAN_INFO_Y, loanInfoY);

        //获取散标产品信息
        paramMap.put(Constants.PRODUCT_TYPE, Constants.S);
        paramMap.put(Constants.PAGE_SIZE, 8);
        List<LoanInfo> loanInfoS = loanInfoService.queryLoanInfoListByProductType(paramMap);
        model.addAttribute(Constants.LOAN_INFO_S, loanInfoS);

        return "index";
    }
}
